// JavaScript Document
setInterval("ReverseToggle('#mainCarousel')", 5000);

/* Method responsible for toggling a pane in a carousel from one to another. */
function ToggleCarousel(id, selected, next, fadeIn) {
	if (id != null && id != undefined) {
		carousel = $(id);
		if (carousel != null && carousel != undefined) {
			carousel.children(".pane").hide();

			if (fadeIn == true) {
				selectedPane.show();
				next.fadeIn();
			}
			else {
				next.show();
				selectedPane.show();
				selectedPane.fadeOut();
			}
			selectedPane.removeClass("selected");
			next.addClass("selected");
			
			/* Get the index of the next pane - and update the naviagtion bubbles to match. */
			var nextIndex = next.index() + 1; // add 1 because .index() is 0-based, not 1-based

			/* Get the casousel navigation bubble links */
			var images = carousel.children(".carouselnavigation").children(".bottom-nav-aligner").children("a");
			
			images.removeClass("current");
			var selectedPaneNavBubble = carousel.children(".carouselnavigation").children(".bottom-nav-aligner").children("a:nth-child(" + nextIndex + ")");
			selectedPaneNavBubble.addClass("current");
		}
	}
}

/* Method used by navigation controls to flip to an arbitrary page */
function GoToPage(id, index) {
	if (id != null && id != undefined) {
		carousel = $(id);
		if (carousel != null && carousel != undefined) {

			selectedPane = carousel.children(".selected");
			var panes = carousel.children(".pane");
			var next = carousel.children(".pane:nth-child(" + index + ")");
			var selectedIndex = selectedPane.index() + 1;

			if (next != null && selectedIndex != null) {
				if (selectedIndex > index) {
					ToggleCarousel(id, selectedPane, next, false);
				}
				else if (selectedIndex < index) {
					ToggleCarousel(id, selectedPane, next, true);                        
				}                   
			}
		} 
	}
}

/* Main carousel animation pane toggle */
function ReverseToggle(id) {
	if (id != null && id != undefined) {
		carousel = $(id);
		if (carousel != null && carousel != undefined) {

			selectedPane = carousel.children(".selected");
			next = selectedPane.prev(".pane");

			if (next.length == 0) {
				next = carousel.children(".pane:last");
				ToggleCarousel(id, selectedPane, next, true);
			}
			else {
				ToggleCarousel(id, selectedPane, next, false);
			}
		}
	}
}

